# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openvdb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=12.0.1
pkgrel=1
pkgdesc="Sparse volume data structure and tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.openvdb.org/"
msys2_repository_url="https://github.com/AcademySoftwareFoundation/openvdb"
msys2_references=(
  'archlinux: openvdb'
)
license=("spdx:MPL-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-blosc"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-imath"
         "${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-nanobind"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-boost: Python bindings"
            "${MINGW_PACKAGE_PREFIX}-python: Python bindings")
checkdepends=("${MINGW_PACKAGE_PREFIX}-cppunit")
source=(https://github.com/AcademySoftwareFoundation/openvdb/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-use-pkgconfig.patch
        002-fix-finding-tbb.patch)
sha256sums=('a3c8724ecadabaf558b6e1bd6f1d695e93b82a7cfdf144b8551e5253340ddce0'
            'aa5593df146487bc28f05f9feafbc11e9cded804a5fd780d7261f5aaa89d752e'
            'a276ec47b897c81537294bb2e7374e151c429308c1ab9f52740a448148a6bbcc')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    001-use-pkgconfig.patch \
    002-fix-finding-tbb.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  local _pyver=$(python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUSE_BLOSC=ON \
    -DUSE_EXR=ON \
    -DUSE_EXPLICIT_INSTANTIATION=OFF \
    -DOPENVDB_CORE_SHARED=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DOPENVDB_BUILD_PYTHON_MODULE=ON \
    -DOPENVDB_BUILD_NANOVDB=ON \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -Dnanobind_DIR="${MINGW_PREFIX}/lib/python${_pyver}/site-packages/nanobind/cmake" \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/openvdb-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # VTK requires FindOpenVDB to be in CMAKE_MODULE_PATH
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/cmake/Modules/
  cp "${pkgdir}"${MINGW_PREFIX}/lib/cmake/OpenVDB/{OpenVDBUtils,FindOpenVDB,FindBlosc}.cmake \
    "${pkgdir}"${MINGW_PREFIX}/share/cmake/Modules/
}
